package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.Coupon;
import com.itheima.mapper.CouponMapper;
import com.itheima.service.SmsCouponService;
import com.itheima.vo.PageBeanVo;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsCouponServiceImpl implements SmsCouponService {
    @Autowired
    CouponMapper smsCouponMapper;

    //根据优惠券名称和类型分页获取优惠券列表
    @Override
    public PageBeanVo list(Integer pageNum, Integer pageSize, String name, Integer type) {
        //创建分页对象
        Page<Coupon> page = new Page<>(pageNum, pageSize);
        //创建条件对象
        LambdaQueryWrapper<Coupon> lqw = new LambdaQueryWrapper<>();
        //追加名称和类型条件
        lqw.like(StringUtils.isNotEmpty(name), Coupon::getName, name);
        lqw.eq(type != null, Coupon::getType, type);
        Page<Coupon> smsCouponPage = smsCouponMapper.selectPage(page, lqw);
        //封装成PageBeanVo返回
        return new PageBeanVo(pageNum, pageSize, smsCouponPage.getTotal(), smsCouponPage.getRecords());
    }

    //添加优惠券
    @Override
    public void save(Coupon smsCoupon) {
        smsCouponMapper.insert(smsCoupon);
    }

    //删除优惠券
    @Override
    public void delete(Integer id) {
        smsCouponMapper.deleteById(id);
    }

    //修改优惠券
    @Override
    public void update(Coupon smsCoupon) {
        smsCouponMapper.updateById(smsCoupon);
    }

    //根据id查询优惠卷
    @Override
    public Coupon selectById(Long id) {
        Coupon smsCoupon = smsCouponMapper.selectById(id);
        return smsCoupon;
    }
}
